$(function () {
  $("#earlyWarningBtn").on("click", function (e) {
    e.preventDefault();
    // 场所占比 整改成效图
    policeRatio();
    // rectificationMap();
  });
  
  // 地图三色预警定位
  $('#earlyWarningModal').on('click', '.earlyWarningPosition', function () {
    policeRatio();
  });
  // 场所占比
  // policeRatio();
  function policeRatio() {
    $.ajax({
      // http://localhost:9092/threeColor/getThreeColorWarningProportion
      url: "/threeColor/getThreeColorWarningProportion",
      type: 'GET',
      async: false, // 同步执行
      success: function (result) {
        console.log(result);
        if (result.status == 200) {
          // 让 图例显示 
          $(".policeRatioBox").find(".policeRatioLegend").show();
          console.log(result); // {0: 0, 1: 7, 2: 2, 3: 9}
          var data = result.data;
          var dataName = [];
          var dataNameObj1 = {
            value: 0,
            name: "一般"
          }
          var dataNameObj2 = {
            value: 0,
            name: "较大"
          }
          var dataNameObj3 = {
            value: 0,
            name: "高危"
          }
          // if(data["一般"]==0) {
          //     dataNameObj1.value = '';
          // } else {
          dataNameObj1.value = data["1"];
          // }
          // if(data["较大"]==0) {
          //     dataNameObj2.value = '';
          // } else {
          dataNameObj2.value = data["2"];
          // }
          // if(data["高危"]==0) {
          //     dataNameObj3.value = '';
          // } else {
          dataNameObj3.value = data["3"];
          // }              
          dataName.push(dataNameObj1);
          dataName.push(dataNameObj2);
          dataName.push(dataNameObj3);

          console.log(dataName);
          // 给图例赋 数值
          $(".normalRiskNum").text(data["1"]);
          $(".moddleRiskNum").text(data["2"]);
          $(".highRiskNum").text(data["3"]);
          // 初始化 实例
          var policeRatio = echarts.init(document.getElementById("policeRatio"));
          option = {
            color: ['#7abd5a', '#fd8731', '#ff3d3d'],
            animation: false,
            // legend: {
            //     orient: 'vertical',
            //     left: 'left',
            //     data:['出租房','公共区域','饭店','居民房']
            // },
            tooltip: {
              trigger: 'item',
              formatter: "{b}:{c}({d}%)"
            },
            series: [{
              type: 'pie',
              radius: '55%',
              center: ['60%', '55%'],
              data: dataName,
              labelLine: {
                normal: {
                  show: false
                }
              },
              label: {
                normal: {
                  position: 'inner',
                },
                // formatter: {b}, 
              },
              itemStyle: {
                normal: {
                  label: {
                    formatter: '{d}%',
                  },
                },
                emphasis: {
                  shadowBlur: 10,
                  shadowOffsetX: 0,
                  shadowColor: 'rgba(0, 0, 0, 0.5)'
                }
              }
            }]
          };
          // 使用制定的配置项和数据显示图表
          policeRatio.setOption(option);
        } else {
          $(".policeRatioBox").find(".policeRatioLegend").hide();
        }

      }
    });
  }

  // 整改成效图
  // rectificationMap();
  function rectificationMap() {
    $.ajax({
      // http://localhost:9092/threeColor/getThreeColorWarningRectifyProportion
      url: "/threeColor/getThreeColorWarningRectifyProportion",
      type: 'GET',
      async: false, // 同步执行
      success: function (result) {
        console.log(result);
        if (result.status == 200) {
          // 让整改文字显示
          $(".rectificationMapBox").find(".rectificationCompleted").removeClass("none");
          console.log(result);
          console.log(result.data); //{一般: 7, 较大: 0, 高危: 0}
          var data = result.data;
          var dataName = [];
          var dataNameObj1 = {
            value: 0,
            name: "一般"
          }
          var dataNameObj2 = {
            value: 0,
            name: "较大"
          }
          var dataNameObj3 = {
            value: 0,
            name: "高危"
          }
          // if(data["一般"]==0) {
          //     dataNameObj1.value = '';
          // } else {
          dataNameObj1.value = data["一般"];
          // }
          // if(data["较大"]==0) {
          //     dataNameObj2.value = '';
          // } else {
          dataNameObj2.value = data["较大"];
          // }
          // if(data["高危"]==0) {
          //     dataNameObj3.value = '';
          // } else {
          dataNameObj3.value = data["高危"];
          // }              
          dataName.push(dataNameObj1);
          dataName.push(dataNameObj2);
          dataName.push(dataNameObj3);

          console.log(dataName);
          var completedPlacesNum = dataNameObj1.value + dataNameObj2.value + dataNameObj3.value;
          $(".completedPlacesNum").text(completedPlacesNum);
          console.log(dataName);
          // var dataText = [];
          // 初始化 实例
          var rectificationMap = echarts.init(document.getElementById("rectificationMap"));
          option = {
            color: ['#7abd5a', '#fd8731', '#ff3d3d'],
            tooltip: {
              trigger: 'item',
              formatter: "{a} <br/>{b}: {c} ({d}%)"
            },
            series: [{
              name: '完成整改',
              type: 'pie',
              radius: ['35%', '45%'],
              center: ['50%', '55%'],
              labelLine: {
                normal: {
                  length: 2,
                  lineStyle: {
                    color: "#2ffa9c",
                  }
                }
              },
              label: {
                normal: {
                  // formatter: function(data) {
                  //     return data.percent.toFixed(0);
                  // },
                  formatter: '{a|{a}}\n{hr|}\n{b|{b}}:{per|{d}%}',
                  borderColor: '#fff',
                  borderWidth: 1,
                  borderRadius: 4,
                  width: 72,
                  height: 40,
                  rich: {
                    a: {
                      color: '#fff',
                      lineHeight: 20,
                      align: 'center',
                      fontSize: 10,
                    },
                    hr: {
                      borderColor: '#fff',
                      width: 72,
                      borderWidth: 0.5,
                      height: 0.5
                    },
                    b: {
                      fontSize: 10,
                      lineHeight: 20,
                      align: 'center',
                      // padding: [0, 10, 0, 0]
                    },
                    per: {
                      padding: [2, 4],
                      borderRadius: 2,
                      fontSize: 10,
                    },
                  }
                },
                position: 'outside'
              },
              data: dataName
            }]
          };
          // 使用制定的配置项和数据显示图表
          rectificationMap.setOption(option);
        } else {
          $(".rectificationMapBox").find(".rectificationCompleted").addClass("none");
        }
      },
      error: function (res) {
        console.log(res);
      }
    });
  }

  // 左边菜单
  // 点击之后字体颜色 和小图标改变
  $(".homePageMenuLeft .firstUl").on("click", "li", function () {
    $(this).find("span").css("color", "#fff");
    $(this).find("a").css("color", "#fff");
    $(this).siblings("li").find("span").css("color", "#346da7");
    $(this).siblings("li").find("a").css("color", "#346da7");
    $(this).find("i").addClass("active");
    $(this).siblings("li").find("i").removeClass("active");
  })
  // 点击下拉框
  $(".homePageMenuLeft .secondUl .secondLiBox").on("click", function () {
    // $(this).siblings("ul").slideDown();
    $(this).siblings("ul").slideToggle();
    $(this).find("s").toggleClass("active");
    // $(this).find("s").addClass("active");
    $(this).parent("li").siblings("li").find("s").removeClass("active");
    $(this).parent().siblings("li").find("ul").slideUp();
    $(this).parent().siblings("li").find("ul").find("li").css("color", "#346da7");
  })
  // $(".homePageMenuLeft .secondUl").on("click", ".secondLiBox" ,function(e){
  //     e.preventDefault();
  //     $(this).siblings("ul").slideToggle();
  // })
  // 点击下拉框下一级的字
  $(".homePageMenuLeft .secondUl li ul").on("click", "li", function () {
    $(this).css("color", "#fff");
    $(this).find("a").css("color", "#fff");
    $(this).siblings("li").css("color", "#346da7");
    $(this).siblings("li").find("a").css("color", "#346da7");
    $(this).parent("ul").parent("li").siblings("li").find("ul").find("li").css("color", "#346da7");
    $(this).parent("ul").parent("li").siblings("li").find("ul").find("li").find("a").css("color", "#346da7");
  })

  // 给搜索框加滚动条
  $(".homepageSearchListItem_scroll").yi_scroll();

  // 点击微型消防站的 右上角弹框
  $(".homePageMenuLeft .fireStationBtnRight").on("click", function () {
    // $(".deviceStatisticsRight").hide();
    map.closeInfoWindow();
    $(".deviceStatisticsRight").find(".titleText").text("微型消防站统计");
    $(".deviceStatisticsRight").find(".deviceAvatar").css("background", "url(/public/images/deviceAvatarFireStationLogo.png) no-repeat");
    $(".policeRatioBox").hide(); // 三色预警隐藏
    $(".deviceStatisticsRight").find(".threeColorText").hide();
    // 设备总数隐藏
    $(".deviceStatisticsRight").find(".configuration-trigger").hide();
    // 设备异常总数隐藏
    $(".deviceStatisticsRight").find(".unitException-trigger").hide();
    // 微型消防站显示
    $(".deviceStatisticsRight").find(".miniFireStationText").show();
    // 楼长制隐藏
    $(".deviceStatisticsRight").find(".buildingSystemText").hide();
    // 协同作战隐藏
    $(".deviceStatisticsRight").find(".cooCSumTextBox").hide();
    // 高层隐藏
    $(".deviceStatisticsRight").find(".highBuildText").hide();
    // 文历保隐藏
    $(".deviceStatisticsRight").find(".wenLiBaoText").hide();
    // 地图筛选按钮隐藏
    $('.pointPositionBox').hide();
  });
  // 楼长制本月检查隐患汇总
  setInterval(function (){
    getFloorHiddenDangerGraphicByOrgIdFn();

  },10000);
  
  //点击楼长按钮
  $('#houseMasterBtn').on('click', function () {
    getFloorHiddenDangerGraphicByOrgIdFn();
  });
  function getFloorHiddenDangerGraphicByOrgIdFn(){
    if(devTypeId=='houseMaster'){
      $.ajax({
        url:'/building/getFloorHiddenDangerGraphicByOrgId',
        success:function(res){
          if(res.status == 200){
            var data = res.data;
            var data1 = data;
            var data2 = [];
            var data3 = data1;
            var dataNum=0;
            for(var i=0;i<data3.length;i++){
              dataNum+=+data3[i].value;
            }
            for(var i=0;i<data1.length;i++){
              // data2.push(Percentage(data1[i].value, dataNum));
              // if(data1[i].name=='防火门管理'){
              //   data1[i].name='防火门管理&nbsp;'
              // data2.push('防火门管理&nbsp;');
              // }else{
    
              //   data2.push(data1[i].name);
              // }
              data2.push(data1[i].name);
              // data3[i].name=Percentage(data1[i].value, dataNum);
            }
            $('.indexRight .houseMasterCheckDangerStyle .totalHiddenTroublNumBox p').eq(0).text(dataNum)
            console.log(dataNum)
            checkDangerCanvasFunc(data1,data2);
          }
        }
      });
    }
  }

  function checkDangerCanvasFunc(data1,data2) {
    // 初始化 实例
    var checkDangerCanvas = echarts.init(document.getElementById("checkDangerCanvas"));
    option = {
      color:['#0096ff','#8fff58','#ff8b2e','#ff3d3d','#7e00ff','#00d8ff'],
      tooltip: {
          trigger: 'item',
          formatter: "{a} <br/>{b}: {c} ({d}%)",
          show:false
      },
      legend: {
        // x : '70%',
        // y : 'center',
        orient: 'horizontal',
        // x : '0%',
        y : 'bottom',
        // itemGap:20,
        itemWidth:10,
        itemHeight:10,
        // width:300,
        height:0,
        // align:'1%',
        textStyle: {color: '#fff'},
        data:data2
      },
      series: [
          {
              name:'本月实时隐患汇总',
              type:'pie',
              radius: ['30%', '40%'],
              label: {
                  normal: {
                      formatter: '{d}%  ',
                      // backgroundColor: 'RGBA(61, 84, 105, 0.9)',
                      borderWidth: 1,
                      borderRadius: 4,
                      rich: {
                          a: {
                              color: '#999',
                              lineHeight: 22,
                              align: 'center'
                          },
                          b: {
                              fontSize: 14,
                              lineHeight: 33
                          },
                      }
                  }
              },
              center : ['50%', '40%'],
              data:data1
          }
      ]
    };
    // 使用制定的配置项和数据显示图表
    checkDangerCanvas.setOption(option);
  }
});